<?php
declare(strict_types=1);
namespace Cache\Driver;
use Cache\CacheInterface;

class Yac implements CacheInterface
{

    /**
     * Yac对象实例
     * @var \Yac|null
     */
    protected ?\Yac $object = null;

    /**
     * 初始化对象
     * @param string|null $prefix
     */
    public function __construct(?string $prefix = null)
    {
        if (!extension_loaded("yac")) exit("yac扩展未安装！");
        $this->object = new  \Yac($prefix?:"");
    }

    /**
     * 自加
     * @param string $name
     * @param int $step
     * @return bool|int
     */
    public function inc(string $name, int $step = 1): bool|int
    {
        $value = $this->get($name) + $step;
        return $this->set($name,$value);
    }

    /**
     * 自减
     * @param string $name
     * @param int $step
     * @return bool|int
     */
    public function dec(string $name, int $step = 1): bool|int
    {
        $value = $this->get($name) - $step;
        return $this->set($name,$value);
    }

    /**
     * @inheritDoc
     */
    public function get(array|string $name): mixed
    {
        // TODO: Implement get() method.
        return $this->object->get($name);
    }

    /**
     * @inheritDoc
     */
    public function set(string $name, mixed $value, ?int $expire = null): bool
    {
        // TODO: Implement set() method.
        return $this->object->set($name, $value, $expire?:0);
    }

    /**
     * @inheritDoc
     */
    public function delete(array|string $name,int $ttl = 0): bool
    {
        // TODO: Implement delete() method.
        return $this->object->delete($name,$ttl);
    }

    /**
     * @inheritDoc
     */
    public function flush(): bool
    {
        // TODO: Implement flush() method.
        return $this->object->flush();
    }

    /**
     * 获取缓存使用情况等信息
     * @return array
     */
    public function info(): array
    {
        return $this->object->info();
    }
}
